/*
 * @Description: 
 * @Version: 
 * @Author: Code Boy
 * @Date: 2021-01-28 09:10:45
 * @LastEditors: Code Boy
 * @LastEditTime: 2021-06-08 13:16:58
 */
#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "tcpip_adapter.h"
#include "esp_eth.h"
#include "esp_event.h"
#include "esp_log.h"
#include "sdkconfig.h"


#include "m_http_client.h"
#include "m_led.h"
#include "m_uart.h"
#include "m_config.h"
#include "m_ble.h"
#include "m_nvs.h"


static const char *TAG = "M_ETHERNET";

uint8_t isWifiConnectOk;

extern xQueueHandle led_evt_queue;
extern led_status_e led_status;

extern ble_esp32_param_t m_ble_esp32_param;

/** Event handler for Ethernet events */
static void eth_event_handler(void *arg, esp_event_base_t event_base,
                              int32_t event_id, void *event_data)
{
    uint8_t mac_addr[6] = {0};
    /* we can get the ethernet driver handle from event data */
    esp_eth_handle_t eth_handle = *(esp_eth_handle_t *)event_data;

    switch (event_id) {
    case ETHERNET_EVENT_CONNECTED:
        esp_eth_ioctl(eth_handle, ETH_CMD_G_MAC_ADDR, mac_addr);
        ESP_LOGI(TAG, "Ethernet Link Up");
        ESP_LOGI(TAG, "Ethernet HW Addr %02x:%02x:%02x:%02x:%02x:%02x",
                 mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);

        #ifndef usr_use_dhcp
        char static_ip_address[16];
        HexArrayToString(m_ble_esp32_param.ble_char5_StaticIP,m_ble_esp32_param.ble_char5_StaticIP_len,static_ip_address);
        tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_ETH);
        tcpip_adapter_ip_info_t eth_info;
        //eth_info.ip.addr = ipaddr_addr("192.168.0.120");
        eth_info.ip.addr = ipaddr_addr(static_ip_address);
        eth_info.netmask.addr = ipaddr_addr("255.255.255.0");
        eth_info.gw.addr = ipaddr_addr("192.168.0.1");
        tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_ETH, (const char*)&eth_info);
        
        // led_status = led_status_wifi_connected;
        // xQueueSend(led_evt_queue, &led_status, NULL);

        isWifiConnectOk = 1;
        //udp_connect_process();
        #endif

        break;
    case ETHERNET_EVENT_DISCONNECTED:
        ESP_LOGI(TAG, "Ethernet Link Down");
        isWifiConnectOk = 0;

        if(led_status != led_status_wifi_connecting)
        {
            led_status = led_status_wifi_connecting;
            xQueueSend(led_evt_queue, &led_status, NULL);
        }
        break;
    case ETHERNET_EVENT_START:
        ESP_LOGI(TAG, "Ethernet Started");
        break;
    case ETHERNET_EVENT_STOP:
        ESP_LOGI(TAG, "Ethernet Stopped");
        break;
    default:
        break;
    }
}

/** Event handler for IP_EVENT_ETH_GOT_IP */
static void got_ip_event_handler(void *arg, esp_event_base_t event_base,
                                 int32_t event_id, void *event_data)
{
    ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data;
    const tcpip_adapter_ip_info_t *ip_info = &event->ip_info;

    ESP_LOGI(TAG, "Ethernet Got IP Address");
    ESP_LOGI(TAG, "~~~~~~~~~~~");
    ESP_LOGI(TAG, "ETHIP:" IPSTR, IP2STR(&ip_info->ip));
    ESP_LOGI(TAG, "ETHMASK:" IPSTR, IP2STR(&ip_info->netmask));
    ESP_LOGI(TAG, "ETHGW:" IPSTR, IP2STR(&ip_info->gw));
    ESP_LOGI(TAG, "~~~~~~~~~~~");

    // led_status = led_status_wifi_connected;
    // xQueueSend(led_evt_queue, &led_status, NULL);

    isWifiConnectOk = 1;
    //udp_connect_process();
}

void mEthernet_init(void)
{
    tcpip_adapter_init();

    ESP_ERROR_CHECK(tcpip_adapter_set_default_eth_handlers());
    ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, NULL));
    #ifdef usr_use_dhcp
    ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, NULL));
    #endif
    eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
    eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();

    esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config);
    esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config);

    esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy);
    esp_eth_handle_t eth_handle = NULL;
    ESP_ERROR_CHECK(esp_eth_driver_install(&config, &eth_handle));
}




